﻿using System;
using System.Collections;
using System.Data;
using SLS.ExClassLib.Message;
namespace SLS.ExClassLib.Events
{
    public class Tracer
    {
        public static readonly Tracer Instance = new Tracer();
        public MessageEventType TracerLevel = MessageEventType.VERBOSE;
        public log4net.ILog m_log;
        public Tracer()
        { }
        #region==================Error=======================
        public static void Error(string from, Exception ex)
        {
            Send(MessageEventType.ERROR, from, ex.Message);
            System.Media.SystemSounds.Beep.Play();
        }

        public static void Error(string from, string message)
        {
            Send(MessageEventType.ERROR, from, message);
            System.Media.SystemSounds.Beep.Play();
        }

        public static void Error(string from, string format, params object[] args)
        {
            Error(from, string.Format(format, args));
        }
        #endregion

        #region==================Info=======================

        public static void Info(string from, string message)
        {
            Send(MessageEventType.INFO, from, message);
        }

        public static void Info(string from, string format, params object[] args)
        {
            Info(from, string.Format(format, args));
        }
        public static void Info(string from, DataRow rowMsg)
        {
            string message = rowMsg.ToString();//rowMsg.ItemArray.ToString();
            Info(from, message);
        }
        #endregion

        #region==================Verbose=======================
        public static void Verbose(string from, string message)
        {
            Send(MessageEventType.VERBOSE, from, message);
        }
        public static void Verbose(string from, DataRow rowMsg)
        {
            string message = rowMsg.ToString();//rowMsg.ItemArray.ToString();
            Verbose(from, message);
        }
        public static void Verbose(string from, Queue queueMsg)
        {
            string message = queueMsg.ToString();
            Verbose(from, message);
        }
        public static void Verbose(string from, DataSet setMsg)
        {
            string message = setMsg.ToString();
            Verbose(from, message);
        }
        public static void Verbose(string from, MessageEx setMsg)
        {
            Verbose(from, setMsg.ToString());
        }
        public static void Verbose(string from, string format, params object[] args)
        {
            Verbose(from, string.Format(format, args));
        }
        #endregion
        #region================== WARN=======================
        public static void Warn(string from, string message)
        {
            Send(MessageEventType.WARN, from, message);
        }

        public static void Warn(string from, string format, params object[] args)
        {
            Warn(from, string.Format(format, args));
        }
        public static void Warn(string from, DataRow rowMsg)
        {
            string message = rowMsg.ToString();
            Warn(from, message);
        }
        #endregion
        #region==================DEBUG=======================
        public static void Debug(string from, string message)
        {
            Send(MessageEventType.DEBUG, from, message);
        }

        public static void Debug(string from, string format, params object[] args)
        {
            Debug(from, string.Format(format, args));
        }
        #endregion
        #region==================EVENTS=======================
        #endregion
        #region==================EVENTS=======================
        #endregion
        #region==================EVENTS=======================
        #endregion

        private static void Send(MessageEventType typ, string from, string message)
        {
            if (typ > (MessageEventType)Tracer.Instance.TracerLevel)
                return;
            MessageInfo info = new MessageInfo(typ, from, message);
            Tracer.Instance.WriteInfoLog(info.ToString());
            //EventDispatcher.Instance.RaiseMessage(info);
        }

        public void WriteInfoLog(string msg)
        {
            if (m_log == null)
                return;
            if (msg.Contains("ERROR:"))
            {
                m_log.Error(msg);
                return;
            }
            m_log.Info(msg);
        }
    }
}